package ru.picman;

import android.graphics.Point;

/**
 * The Bird. This Unit is typically controlled by the player and
 * provides the functionality the player needs to correctly control
 * the bird and complete the level.
 * 
 * @author Matthijs
 */
public class Bird
             extends Unit
{
	/**
	 * Constructs the bird. The moving speed is set (to 0.2) and the
	 * AI is set to BirdAI.
	 */
	public Bird() {
		super(EntityType.BIRD);
		
		setMoveStep(.2f);
		setAIThread(new BirdAI(this));
	}
	
	//
	
	/**
	 * Sets the player for this bird.
	 * 
	 * @param player The player
	 */
	public void setPlayer(Player player) {
		if (player == null)
			throw new NullPointerException();
		this.player = player;
	}
	
	/**
	 * The bird has died, {@link Player#die()} is called.
	 */
	public void die() {
		player.die();
	}
	
	/**
	 * Makes this entity immortal, or not.<br />
	 * <b>NOTE:</b> This does not mean the entity will flash.
	 * For that, see {@link BirdFlasher}
	 * 
	 * @param immortal true to make it immortal
	 */
	public void setImmortal(boolean immortal) {
		this.immortal = immortal;
	}
	
	/**
	 * Checks if this entity is immortal.
	 * 
	 * @return true if it is
	 */
	public boolean isImmortal() {
		return immortal;
	}
	
	public void moved(int oldX, int oldY) {
		Point p = getPositionI();
		if (!world.isValid(p.x,p.y))
			return;
		Egg egg = world.getEgg(p.x,p.y);
		if (egg != null && collides(egg)) {
			egg.take();
			player.incScore();
			if (egg.getAction() != null)
				player.incScore();
			Egg[][] eggs = world.getEggs();
			for (int x = 0; x < eggs.length; ++x)
				for (int y = 0; y < eggs.length; ++y)
					if (eggs[x][y] != null && eggs[x][y].getEggType() == Egg.EGG_WHITE && !eggs[x][y].isTaken())
						return;
			world.getGame().levelComplete();
		}
	}
	
	public void hit(int x, int y) {
		getWorld().getBlock(x,y).hit();
	}
	
	//
	
	private Player  player;
	private boolean immortal;
	
}
